package com.cdshangpai.utils.api;

import android.app.Activity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cdshangpai.utils.converts.JsonConvert;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class HttpResponse {
    private Activity activity;
    private HttpCallback httpCallback;
    private Boolean success;
    private String errmsg;
    Class responseClz;
    Class listClz;
    Object data;

    public HttpResponse(Activity activity,HttpCallback httpCallback,Class responseClz)
    {
        this.activity=activity;
        this.httpCallback=httpCallback;
        this.responseClz=responseClz;
        this.data=data;
    }

    public HttpResponse(Activity activity,HttpCallback httpCallback,Class responseClz,Object data)
    {
        this.activity=activity;
        this.httpCallback=httpCallback;
        this.responseClz=responseClz;
        this.data=data;
    }

    public void setResponseData(Object data){
        if(data==null)
        {
            this.data=null;
        }
        else if(data instanceof String)
        {
            this.data=data;
        }
        else if(data instanceof JSONObject)
        {
            this.data= JsonConvert.json2Bean((JSONObject)data,responseClz);
        }
        else if(data instanceof JSONArray)
        {
            this.data=JsonConvert.json2List(data.toString(),listClz);
        }
    }
    public void callback(){
        httpCallback.callback(this);
    }
}
